/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title volatile 关键字并发测试
 * @Description 20个线程同时累加 volatile修饰的常量，最终查看结果是否为期望的值
 * @Package concurrent.volatiletest
 * @date 2021/3/26 1:44 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package concurrent.mthread.automicclass;

import java.util.concurrent.atomic.AtomicInteger;

public class AutomicIncrementTest {


    private static AtomicInteger race = new AtomicInteger(0);

    private static final int THREADS_COUNT = 20;

    public  static void increase() {
        race.incrementAndGet();
    }

    public static void main(String[] args) {

        Thread[] threads = new Thread[THREADS_COUNT];
        for (int i = 0; i < THREADS_COUNT; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    increase();
                }
            });

            threads[i].start();
        }

        while (Thread.activeCount() > 1) {
            Thread.yield();
        }

        System.out.printf("累加结果: %d \n", race.get());

    }


}
